package org.wevil.controller.sys;

import org.wevil.entity.sys.SysUser;
import org.wevil.params.page.BasePP;
import org.wevil.repo.sys.SysUserRepo;
import org.wevil.service.sys.DownloadService;


import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/** 下载 */
@RestController
@RequestMapping("download")
public class DownloadController {

    @Resource
    SysUserRepo sysUserRepo;
    @Resource
    DownloadService service;

    /** 用户信息 */
    @PostMapping("/userInfo")
    public void userInfo(){
        List<SysUser> list = sysUserRepo.findAll();
        service.exportList(list, SysUser.class, "用户信息.xlsx");
    }

    /** 测试下载文件流，带请求参数的 */
    @PostMapping("/testParam")
    public void testParam(@RequestBody BasePP param) {
        List<SysUser> list = sysUserRepo.findAll();
        service.exportList(list, SysUser.class, "用户信息.xlsx");
    }

    /** 测试ResponseEntity下载 */
    @PostMapping("/testEntity")
    public ResponseEntity<byte[]> testDownFile() throws IOException {
        InputStream in = this.getClass().getResourceAsStream("/downloads/down.txt");
        byte[] bytes = FileCopyUtils.copyToByteArray(in);
        HttpHeaders header = new HttpHeaders();
        header.add("Content-Disposition", "attachment;filename=down.txt");
        return new ResponseEntity<>(bytes, header, HttpStatus.OK);
    }
}
